package com.gxuwz.attend.context;

import com.gxuwz.attend.jdbc.DbManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;

/**
 * 访问IoC容器工具
 * @Author amu_1115@126.com
 */
public final class AppContext {
    private static ApplicationContext CONTEXT;
    private static final String CONFIGURATION_XML ="classpath:applicationContext.xml";
    //静态代码块
    static {
        CONTEXT =new ClassPathXmlApplicationContext(CONFIGURATION_XML);
    }

    /**
     * 从IOC容器中获取Bean
     * @param tClass
     * @return
     * @param <T>
     */
    public static  <T>T getBean(Class<T> tClass){
       return CONTEXT.getBean(tClass);
    }

    public  static void main(String[]args)throws Exception{
        DbManager dbManager =(DbManager)AppContext.getBean(DbManager.class);
        System.out.println("dbManager:"+dbManager);
        //测试连接参数
        Connection conn = dbManager.getConn();
        System.out.println("conn:"+conn);
        //输出连接数据库的名称
        System.out.println("catalog:"+conn.getCatalog());
    }

}
